<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\PaymentThirdCode;
use App\Models\PaymentThirdPay;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class PaymentThirdCodeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PaymentThirdCode(), function (Grid $grid) {
            $grid->model()->with('payment')
                ->orderBy('sort');

            $grid->column('id')->sortable();
            $grid->column('payment.title',trans('payment-third-pay.fields.title'));
            $grid->column('title');
            $grid->column('is_open')->switch();
            $grid->column('pay_type');
            $grid->column('pay_code');
            $grid->column('upper')->display(function ($var) {
                return $var / 100;
            });
            $grid->column('lower')->display(function ($var) {
                return $var / 100;
            });
            $grid->column('sort');
            $grid->column('remark');
            //$grid->column('created_at');
            //$grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('payment.id', trans('payment-third-pay.fields.title'))->select($this->payments());

            });

            $grid->disableViewButton();
            $grid->showQuickEditButton();


        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PaymentThirdCode(), function (Form $form) {
            //$form->display('id');
            $form->select('payment_third_pay_id')->options($this->payments())->required();
            $form->text('title')->required();
            $form->switch('is_open')->default(true);
            $form->text('pay_type')->required();
            $form->text('pay_code')->required();
            $form->text('upper')->required();
            $form->text('lower')->required();
            $form->text('sort')->default(0)->required();
            $form->text('remark');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function payments(): array
    {
        $hierarchies = PaymentThirdPay::all()->toArray();
        $data = [];
        foreach ($hierarchies as $k => $v) {
            $data[$v['id']] = $v['server_name'];
        }
        return $data;
    }

}
